﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace ExcelUtilities.Cfg
{
    /// <summary>
    /// configuration reader
    /// </summary>
    class CfgReader
    {
        /// <summary>
        /// 读取配置
        /// </summary>
        /// <param name="cfgFile">配置文件</param>
        /// <param name="workbookName">配置项名称</param>
        /// <returns></returns>
        public static Workbook Get(string cfgFile, string workbookName)
        {
            XmlSerializer xmlReader = new XmlSerializer(typeof(Excel));
            using (var cfgFileStream = new FileStream(cfgFile, FileMode.Open, FileAccess.Read))
            {
                var excel = xmlReader.Deserialize(cfgFileStream) as Excel;
                cfgFileStream.Close();
                return excel.Workbooks.SingleOrDefault(m => m.Name.ToLower() == workbookName.ToLower());
            }
        }
    }
}